import java.util.Arrays;

public class TestDemo2 {

    public static void main(String[] args) {
        int[][] arr = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12}
        };
        for (int row = 0; row < arr.length; row++) {
            for (int col = 0; col < arr[row].length; col++) {
                System.out.printf("%d\t", arr[row][col]);
            }
            System.out.println("");
        }
    }

    public static void main9(String[] args) {

        int[] arr = null;
        System.out.println(arr[0]);

    }

    public static void main8(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int len = arr.length;
        // for循环遍历数组
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        // for-each遍历数组
        for (int e : arr) {
            System.out.println(e);
        }

    }

    public static void main7(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8};

        int[] arr2 = new int[]{1,2,3,4,5};

        int[] arr3 = new int[5];
        arr3 = new int[]{1,2,3,4,5}; // arr3的初始化

        int[] array;
        array = new int[]{1,2,3,4};

        System.out.println(array[10]); // 数组越界

//        int[] arr = {1, 2, 3, 4, 5};
//        System.out.println(arr[10]); // 数组只有5个元素，访问下标为10的元素会产生越界异常



    }

    // 拷贝数组
    /**
     * 用array.clone进行拷贝
     * @param args
     */
    public static void main6(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = array.clone();
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }

    /**
     * 用System.arraycopy进行拷贝
     * @param args
     */
    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = new int[array.length];
        System.arraycopy(array, 0, copy, 0, array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }

    /**
     * 用Arrays.copyOf进行拷贝
     * @param args
     */
    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = Arrays.copyOf(array,array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }

    /**
     * 用for循环进行拷贝
     * @param args
     */
    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }

    // 数组转字符串,实现toString
    public static String myToString(int[] arr) {
        String str = "";
        str += "[";
        for (int i = 0; i < arr.length-1; i++) {
            str = str + arr[i] + ", ";
        }
        str = str + arr[arr.length-1] + "]";
        return str;
    }

    public static void main2(String[] args) {
        int[] arr = {1,2,3,4,5};
        System.out.println(Arrays.toString(arr));
        String str = myToString(arr);
        System.out.println(str);
    }

    public static void func1(int[] array) {
        array = new int[] {24,567,24};
    }

    public static void func2(int[] array) {
        array[0] = 200;
    }

    public static void main1(String[] args) {
        int[] array1 = {1,2, 3, 4, 5};
        func1(array1);
        System.out.println(Arrays.toString(array1));

        int[] array2 = {6, 7, 8, 9, 10};
        func2(array2);
        System.out.println(Arrays.toString(array2));
    }
}
